package com.example.venue_reservation_service.vo.excel;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.*;
import com.alibaba.excel.enums.BooleanEnum;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import com.example.venue_reservation_service.domain.Payment;
import lombok.Data;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 账单导出数据对象
 */
@Data
@ContentFontStyle(fontName = "楷体", fontHeightInPoints = 16) // 内容字体
@HeadFontStyle(fontName = "楷体", fontHeightInPoints = 16, bold = BooleanEnum.TRUE) // 表头字体
@HeadStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER) // 表头居中
@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER) // 内容居中
@ContentRowHeight(25) // 内容行高
@HeadRowHeight(30) // 表头行高
@ColumnWidth(30) // 默认列宽
public class PaymentExportDTO {

    @ExcelProperty(value = "编号", index = 0)
    private String id;

    @ExcelProperty(value = "用户编号", index = 1)
    @ColumnWidth(15) // 单独设置列宽
    private String userId;

    @ExcelProperty(value = "交易金额(元)", index = 2)
    @ColumnWidth(18)
    private String amount;

    @ExcelProperty(value = "交易类型", index = 3)
    private String situation;

    @ExcelProperty(value = "交易时间", index = 4)
    @ColumnWidth(25)
    private String time;

    // 将实体对象转换为导出DTO
    public static PaymentExportDTO fromEntity(Payment payment) {
        PaymentExportDTO dto = new PaymentExportDTO();
        dto.setId(String.valueOf(payment.getId()));
        dto.setUserId(String.valueOf(payment.getUserId()));
        dto.setAmount(String.format("¥%.2f", payment.getAmount()));
        dto.setSituation(translateSituation(payment.getSituation()));
        dto.setTime(payment.getTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return dto;
    }

    // 交易类型翻译
    private static String translateSituation(String situation) {
        switch (situation) {
            case "支出": return "预约花费";
            case "支入": return "账户充值";
            case "超时": return "未签到超时";
            case "取消": return "预约取消";
            default: return situation;
        }
    }
}